package org.platform.core;

import lombok.extern.slf4j.Slf4j;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * Created by mabb on 15-1-22.
 *
 * @author
 */
@Slf4j
@Service
public class JobManager {
    @Autowired
    private Scheduler scheduler;

    public void unRegister(JobDetail jd) {
        try {
            if (scheduler.checkExists(jd.getKey())) {
                scheduler.deleteJob(jd.getKey());
            }
        } catch (SchedulerException e) {
            log.error("无法删除任务" + jd.getKey());
            log.error("JobManager.deleteJob", e);
        }
    }

    public void register(JobDetail jd, Trigger trigger) {
        try {
            if (!scheduler.checkExists(jd.getKey())) {
                scheduler.scheduleJob(jd, trigger);
            }
        } catch (SchedulerException e) {
            log.error("无法调度任务" + jd.getKey());
            log.error("JobManager.register", e);
        }
    }

}
